<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($this->adminUrl, $this->lang('breadcrumb_browse'));

$browsePhotosGallery ='';

if(get('filename')!='' && get('filepath')!=''){
	// show the file contents
	$file = JFX_Data::getFile(get('filepath'), get('filename'), $this->keyname);
	while(ob_get_level()>0){
		ob_end_clean();
	}
	header('Content-type: '.$file->meta['mime']);
	if(get('maxwidth')!='' && get('maxheight')!=''){
		// need to resize the image
		$fc = $file->getContents();
		$tmpName = tempnam($CONFIG->tmpDir, $file->meta['friendly_name']);
		$fh = fopen($tmpName, 'w');
		fwrite($fh, $fc);
		fclose($fh);
		JFX_Image::resize($tmpName, get('maxwidth'), get('maxheight'));
		die(file_get_contents($tmpName));
	}else{
		die($file->getContents());
	}

}


if(get('folder')!=''){
	// show the list of photos by ajax request

	$folderName = get('folder');
	$folderPath = $this->getFolderPath($folderName);
	$realRes = array();
	$res = JFX_Data::getModuleFilesByPrefix($this->keyname, $folderPath);

	foreach($res as $k=>$photo){
		$realRes[$k] = $photo->meta;
		$realRes[$k]['urlSmall'] = $this->imageUrl.'/serve.php?filepath='.$photo->meta['filepath'].'&amp;filename='.$photo->meta['filename'].'&amp;maxwidth=80&amp;maxheight=80';
		$realRes[$k]['filepath'] = $photo->getAbsolutePath();
	}

	$SMARTY->assign('photos', $realRes);
	$SMARTY->assign('hasPhotos', (bool) (is_array($res) && count($res)>0));
	$SMARTY->assign('iframeUrl', $CONFIG->baseUrl.'/jfx/phpimageeditor/index.php?imagesrc='.$realRes[0]['filepath']);
	$browsePhotosGallery = $this->fetchTemplate('/admin/browse.tpl');

}


$tree = $this->getFolderHeirarchy();

$folderUrl = $this->adminUrl.'/browse/?folder=:|folder|:';
$click = '';

// show the folder tree
echo '<div class="jfx-imager-folder-tree"><h3>'.$this->lang('choose_a_folder').'</h3>';
echo '<ul id="JFX_Module_Imager_FolderTreeview" class="treeview" style="float: left">'.
$this->makeTree($tree, $this, false, $folderUrl, $folderUrl, $click, $click).'</ul></div>';

echo $browsePhotosGallery;

echo '<div style="clear:both;"></div>';

$script = <<<JS
<script type="text/javascript" language="JavaScript">

$('#JFX_Module_Imager_FolderTreeview').treeview({
			persist: "location",
			collapsed: true,
			unique: true
		});

function jfx_imager_edit_photo(filepath){
	var src = '{$CONFIG->baseUrl}/jfx/phpimageeditor/index.php?imagesrc='+filepath;
	$('#jfx-imager-phpeditor-iframe > iframe').attr('src', src);
}

$('#jfx-imager-phpeditor-iframe').resizable();

</script>
JS;

echo $script;



